package com.mbuswidget;

import java.util.ArrayList;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MBusWidgetConfigurationStopsAddStopByAllStops extends Activity {
	private int widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
	private ListView itemList;
	ArrayList<String> listOfStops;

	// Service related stuff...
	private MBusWidgetService mbusBoundService;
	private boolean mIsBound;
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			// This is called when the connection with the service has been
			// established, giving us the service object we can use to
			// interact with the service. Because we have bound to a explicit
			// service that we know is running in our own process, we can
			// cast its IBinder to a concrete class and directly access it.
			mbusBoundService = ((MBusWidgetService.LocalBinder) service)
					.getService();
			// mService = new Messenger((MBusWidgetService.LocalBinder)service);

			// Tell the user about this for our demo.
//			Toast.makeText(MBusWidgetConfigurationStopsAddStopByAllStops.this,
//					R.string.local_service_connected, Toast.LENGTH_SHORT)
//					.show();

			UpdateItemList();
		}

		public void onServiceDisconnected(ComponentName className) {
			// This is called when the connection with the service has been
			// unexpectedly disconnected -- that is, its process crashed.
			// Because it is running in our same process, we should never
			// see this happen.
			mbusBoundService = null;

			// This is called when the connection with the service has been
			// unexpectedly disconnected -- that is, its process crashed.

//			Toast.makeText(MBusWidgetConfigurationStopsAddStopByAllStops.this,
//					R.string.local_service_disconnected, Toast.LENGTH_SHORT)
//					.show();
		}
	};

	void doBindService() {
		// Establish a connection with the service. We use an explicit
		// class name because we want a specific service implementation that
		// we know will be running in our own process (and thus won't be
		// supporting component replacement by other applications).
		bindService(new Intent(
				MBusWidgetConfigurationStopsAddStopByAllStops.this,
				MBusWidgetService.class), mConnection, Context.BIND_AUTO_CREATE);
		mIsBound = true;
	}

	void doUnbindService() {
		if (mIsBound) {
			// Detach our existing connection.
			unbindService(mConnection);
			mIsBound = false;
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectstopby);
		setResult(RESULT_CANCELED);

		TextView topLabel = (TextView) findViewById(R.id.Toplbl);
		topLabel.setText(R.string.chooseStop);
		listOfStops = new ArrayList<String>();

		widgetID = getIntent().getExtras().getInt(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);

		itemList = (ListView) findViewById(R.id.itemList);

		doBindService();

		itemList.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				// This is show ll stops
				Intent intent =  new Intent(MBusWidgetConfigurationStopsAddStopByAllStops.this, MBusWidgetConfigurationStopsAddStop.class);
				intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
				String[] stringArray = new String[1];
				stringArray[0] = listOfStops.get((int) id);

				intent.putExtra(MBusWidgetConstants.INTENT_SELECTED_STOPS,
						stringArray);
				setResult(RESULT_OK, intent);
				doUnbindService();
				finish();
			}
		});
	}
	
	@Override
	public void onDestroy() {
		doUnbindService();
		super.onDestroy();
	}

	public void UpdateItemList() {
		MBusInfo mbusinfo = null;
		if (mbusBoundService != null) {
			mbusinfo = mbusBoundService.getBusInfo();
		}
		if (mbusinfo != null) {
			listOfStops = mbusinfo.getStopNamesAll();
		}

		itemList.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, listOfStops));
	}

}
